﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Windows;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;

namespace TheOliver.Phone.Tools
{
    public class Helper
    {
        public static NavigationService GetNavigationService(FrameworkElement fe)
        {
            if (fe != null)
            {
                if (fe is PhoneApplicationPage)
                {
                    PhoneApplicationPage pap = fe as PhoneApplicationPage;
                    return pap.NavigationService;
                }
                else if (fe.Parent != null)
                {
                    return GetNavigationService(fe.Parent as FrameworkElement);
                }
            }
            return null;
        }

        internal static PhoneApplicationPage GetParentPage(FrameworkElement fe)
        {
            if (fe != null)
            {
                if (fe is PhoneApplicationPage)
                {
                    PhoneApplicationPage pap = fe as PhoneApplicationPage;
                    return pap;
                }
                else if (fe.Parent != null)
                {
                    return GetParentPage(fe.Parent as FrameworkElement);
                }
            }
            return null;
        }
    }
}
